package com.jindun.paper.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.jindun.paper.dao.ClientRepository;
import com.jindun.paper.model.Client;
import com.jindun.paper.service.ClientService;
import com.jindun.paper.vo.ClientVo;

@Component
public class ClientServiceImpl implements ClientService {

	@Autowired
	private ClientRepository clientRepository;
	
	@Autowired
	private MonthlyCalculator monthlyCalculator;
	
	@Autowired
	private EntityManager entityManager;
	
	
	
	
	
	@Override
	@Transactional(readOnly=true)
	public Client getOne(Integer clientId) {
		Client client =  this.clientRepository.getOne(clientId);
		Hibernate.initialize(client);
	  
		return client;
		
	}

	@Override
	@Transactional
	public void save(Client bean) {
		Client client = null;
		if (bean.getId() != null) {
			client = this.clientRepository.getOne(bean.getId());
		} else {
			client = new Client ();
			
		}
		client.setBankAccount(bean.getBankAccount());
		client.setClientName(bean.getClientName());
		client.setClientType(bean.getClientType());
		client.setIdCardNo(bean.getIdCardNo());
		client.setLocation(bean.getLocation());
		client.setMainClient(client.getParent() == null);
		client.setParent(bean.getParent());
		client.setStatus(bean.getStatus());
		
		this.clientRepository.save(client);
		
	}

	@Override
	@Transactional(readOnly=true)
	public Date[] calcMonthlyBeginDate(Integer clientId, Date currentDate) {

		Client client = this.clientRepository.getOne(clientId);
		int begin = client.getClientType().getMonthlyBegin() ;
		return monthlyCalculator.calc(begin, currentDate);
		
	}

	@Override
	@Transactional(readOnly=true)
	public List<ClientVo> findAll() {
		List<Client> clients = this.clientRepository.findAll();
		List<ClientVo> clientVoList = new ArrayList<ClientVo>(clients.size());
		for (Client client : clients) {
			clientVoList.add(this.convert(client));
		}
		return clientVoList;
	}
	
	
	private ClientVo convert (Client client) {
		ClientVo vo = new ClientVo ();
		vo.setClientName(client.getClientName());
		vo.setClientType(client.getClientType().getName());
		vo.setId(client.getId());
		vo.setIdCardNo(client.getIdCardNo());
		vo.setStatus(client.getStatus());
		if (client.getParent() != null) {
			vo.setParentClientName(client.getParent().getClientName());
		}
		vo.setBankAccount(client.getBankAccount());
		if (client.getLocation() != null) {
			vo.setFromWhere(client.getLocation().getValue());
		}
		
		return vo;
	}

	@Override
	public List<ClientVo> findMainClient() {
		List<Client> clients = this.clientRepository.findByMainClientOrderByClientName(true);
		List<ClientVo> clientVoList = new ArrayList<ClientVo>(clients.size());
		for (Client client : clients) {
			clientVoList.add(this.convert(client));
		}
		return clientVoList;
	}

	@Override
	public boolean isClientUnique(Integer clientId, String clientName) {
		
		StringBuilder jql = new StringBuilder();
		jql.append("from Client c where c.clientName = ? ");
		if (clientId != null) {
			jql.append(" and c.id != ?");
		}
		
		Query query = this.entityManager.createQuery(jql.toString());
		query.setParameter(1, clientName);
		
		if (clientId != null) {
			query.setParameter(2, clientId);
		}
		
		
		return query.getResultList().size() == 0;
	}
	
	
	
	

}
